import ecStat from 'echarts-stat'

export const getScatterOption = (day, chartData) => {

  const [dDatas, vDatas] = chartData
  const dLinearDatas = ecStat.regression('linear', dDatas, 2)
  const vLinearDatas = ecStat.regression('linear', vDatas, 2)
  const [dColor, vColor] = ['#c23531', '#62a0a7']

  return {
    title: {
      text: `${day}`,
    },
    grid: {
      left: '3%',
      right: '7%',
      bottom: '3%',
      containLabel: true
    },
    tooltip: {
      showDelay: 0,
      formatter: function (params) {
        if (params.value.length > 1) {
          return params.seriesName + ' :<br/>'
            + params.value[0] + 'm/s '
            + params.value[1] + 'cm '
        }
        else {
          return params.name + '：' + params.value + 'cm '
        }
      },
      axisPointer: {
        show: true,
        type: 'cross',
        lineStyle: {
          type: 'dashed',
          width: 1
        }
      }
    },

    legend: {
      data: ['降落偏差', '视觉定位偏差', '\n', '降落回归线', '视觉回归线'],
      right: 0
    },
    xAxis: [
      {
        type: 'value',
        name:'风速',
        scale: true,
        min: (value) => value.min,
        axisLabel: {
          formatter: '{value} m/s'
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '偏差',
        scale: true,
        axisLabel: {
          formatter: '{value} cm'
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '降落偏差',
        type: 'scatter',
        data: dDatas,
        markPoint: {
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        },
        itemStyle: {
          color: dColor
        }
      },
      {
        name: '降落回归线',
        type: 'line',
        showSymbol: false,
        data: dLinearDatas.points,
        lineStyle: {
          type: 'dash',
          color: dColor
        }
      },
      {
        name: '视觉定位偏差',
        type: 'scatter',
        data: vDatas,
        markPoint: {
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        },
        itemStyle: {
          color: vColor
        }
      },
      {
        name: '视觉回归线',
        type: 'line',
        showSymbol: false,
        data: vLinearDatas.points,
        lineStyle: {
          type: 'dash',
          color: vColor
        }
      }
    ]
  }

}

export const getLineOption = (chartData) => {
  const tColor = '#f0407b'
  const tLinearDatas = ecStat.regression('linear', chartData, 2)

  return {
    grid: {
      left: '3%',
      right: '7%',
      bottom: '3%',
      containLabel: true
    },
    tooltip: {
      showDelay: 0,
      formatter: function (params) {
        if (params.value.length > 1) {
          return params.seriesName + ' :<br/>'
            + params.value[0] + 'm/s '
            + params.value[1] + 's '
        }
        else {
          return params.name + '：' + params.value + 's '
        }
      },
      axisPointer: {
        show: true,
        type: 'cross',
        lineStyle: {
          type: 'dashed',
          width: 1
        }
      }
    },
    legend: {
      data: ['降落耗时'],
      right: 0
    },
    xAxis: [
      {
        type: 'value',
        name: '风速',
        scale: true,
        min: (value) => value.min,
        axisLabel: {
          formatter: '{value} m/s'
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '耗时',
        scale: true,
        axisLabel: {
          formatter: '{value} s'
        },
        splitLine: {
          show: false
        }
      }
    ],
    series: [
      {
        name: '降落耗时',
        type: 'scatter',
        data: chartData,
        itemStyle: {
          color: tColor
        },
        markPoint: {
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        }
      }, {
        name: '耗时回归线',
        type: 'line',
        showSymbol: false,
        data: tLinearDatas.points,
        lineStyle: {
          type: 'dash',
          color: tColor
        }
      }
    ]

  }
}

const randomColor = () => {
  const r = Math.floor(Math.random()*255);
  const g = Math.floor(Math.random()*255);
  const b = Math.floor(Math.random()*255);
  return `rgb(${r}, ${g}, ${b}, 1)`
}

export const getTotalScatterOption = (chartDatas) => {

  const series: any = []
  for (const n of chartDatas) {
    const [dDatas, vDatas] = n
    const dLinearDatas = ecStat.regression('linear', dDatas, 2)
    const vLinearDatas = ecStat.regression('linear', vDatas, 2)
    const [dColor, vColor] = [randomColor(), randomColor()]
    series.push({
        name: '降落偏差',
        type: 'scatter',
        data: dDatas,
        markPoint: {
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        },
        itemStyle: {
          color: dColor
        }
      },
      {
        name: '降落回归线',
        type: 'line',
        showSymbol: false,
        data: dLinearDatas.points,
        lineStyle: {
          type: 'dash',
          color: dColor
        }
      },
      {
        name: '视觉定位偏差',
        type: 'scatter',
        data: vDatas,
        markPoint: {
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        },
        itemStyle: {
          color: vColor
        }
      },
      {
        name: '视觉回归线',
        type: 'line',
        showSymbol: false,
        data: vLinearDatas.points,
        lineStyle: {
          type: 'dash',
          color: vColor
        }
      })
  }

  return {
    title: {
      text: `汇总展示`,
    },
    grid: {
      left: '3%',
      right: '7%',
      bottom: '3%',
      containLabel: true
    },
    tooltip: {
      showDelay: 0,
      formatter: function (params) {
        if (params.value.length > 1) {
          return params.seriesName + ' :<br/>'
            + params.value[0] + 'm/s '
            + params.value[1] + 'cm '
        }
        else {
          return params.name + '：' + params.value + 'cm '
        }
      },
      axisPointer: {
        show: true,
        type: 'cross',
        lineStyle: {
          type: 'dashed',
          width: 1
        }
      }
    },
    xAxis: [
      {
        type: 'value',
        name:'风速',
        scale: true,
        min: (value) => value.min,
        axisLabel: {
          formatter: '{value} m/s'
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '偏差',
        scale: true,
        axisLabel: {
          formatter: '{value} cm'
        },
        splitLine: {
          show: false
        }
      }
    ],
    series
  }

}

export const getTotalLineOption = (chartDatas) => {
  const series: any = []
  for (const n of chartDatas) {
    const tDatas = n[2]
    const tLinearDatas = ecStat.regression('linear', tDatas, 2)
    const tColor = randomColor()
    series.push({
      name: '降落耗时',
      type: 'scatter',
      data: tDatas,
      itemStyle: {
        color: tColor
      },
      markPoint: {
        data: [
          {type: 'max', name: '最大值'},
          {type: 'min', name: '最小值'}
        ]
      }
    }, {
      name: '耗时回归线',
      type: 'line',
      showSymbol: false,
      data: tLinearDatas.points,
      lineStyle: {
        type: 'dash',
        color: tColor
      }
    })
  }

  return {
    grid: {
      left: '3%',
      right: '7%',
      bottom: '3%',
      containLabel: true
    },
    tooltip: {
      showDelay: 0,
      formatter: function (params) {
        if (params.value.length > 1) {
          return params.seriesName + ' :<br/>'
            + params.value[0] + 'm/s '
            + params.value[1] + 's '
        }
        else {
          return params.name + '：' + params.value + 's '
        }
      },
      axisPointer: {
        show: true,
        type: 'cross',
        lineStyle: {
          type: 'dashed',
          width: 1
        }
      }
    },

    xAxis: [
      {
        type: 'value',
        name: '风速',
        scale: true,
        // min: (value) => value.min,
        axisLabel: {
          formatter: '{value} m/s'
        },
        splitLine: {
          show: false
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '耗时',
        scale: true,
        axisLabel: {
          formatter: '{value} s'
        },
        splitLine: {
          show: false
        }
      }
    ],
    series
  }
}

export const getTotalChartOptions = (chartDatas) => {
  return [getTotalScatterOption(chartDatas), getTotalLineOption(chartDatas)]
}

export const getSpeedOption = (speed) => {
  return {
    series: [{
      type: 'gauge',
      startAngle: 180,
      endAngle: 0,
      min: 0,
      max: 20,
      splitNumber: 5,
      itemStyle: {
        color: '#aef',
        shadowColor: '#303336',
        shadowBlur: 10,
        shadowOffsetX: 2,
        shadowOffsetY: 2
      },
      progress: {
        show: true,
        roundCap: true,
        width: 6
      },
      pointer: {
        icon: 'path://M2090.36389,615.30999 L2090.36389,615.30999 C2091.48372,615.30999 2092.40383,616.194028 2092.44859,617.312956 L2096.90698,728.755929 C2097.05155,732.369577 2094.2393,735.416212 2090.62566,735.56078 C2090.53845,735.564269 2090.45117,735.566014 2090.36389,735.566014 L2090.36389,735.566014 C2086.74736,735.566014 2083.81557,732.63423 2083.81557,729.017692 C2083.81557,728.930412 2083.81732,728.84314 2083.82081,728.755929 L2088.2792,617.312956 C2088.32396,616.194028 2089.24407,615.30999 2090.36389,615.30999 Z',
        length: '75%',
        width: 10,
        offsetCenter: [0, '5%']
      },
      axisLine: {
        roundCap: true,
        lineStyle: {
          width: 10
        }
      },
      axisTick: {
        splitNumber: 2,
        lineStyle: {
          width: 2,
          color: '#999'
        }
      },
      splitLine: {
        length: 8,
        lineStyle: {
          width: 3,
          color: '#999'
        }
      },
      axisLabel: {
        distance: 12,
        color: '#fff',
        fontSize: 10
      },
      title: {
        show: false
      },
      detail: {
        backgroundColor: '#fff',
        borderColor: '#999',
        borderWidth: 2,
        width: 55,
        lineHeight: 10,
        height: 15,
        borderRadius: 8,
        offsetCenter: [0, '45%'],
        valueAnimation: true,
        formatter: function (value) {
          return '{value|' + value.toFixed(0) + '}{unit|m/s}';
        },
        rich: {
          value: {
            fontSize: 12,
            fontWeight: 'bolder',
            color: '#777'
          },
          unit: {
            fontSize: 12,
            color: '#999',
            padding: [0, 0, -20, 10]
          }
        }
      },
      data: [{
        value: speed
      }]
    }]
  }
}
